/*
 * 监控区主页 WebSocket
 * @author Zdream
 * @since 0.2.0
 */

monitor.web = {
	
	/*
	 * onload 事件之后有效, 之前全为 null
	 */
	ws : null,
	
	handler : {},
	
	/**
	 * 选择处理的回调函数, 并让其处理
	 */
	selectHandler(data) {
		var type = data.type;
		var h = this.handler[type];
		
		if (h != null) {
			h(data);
		} else {
			console.log("从服务器接收到以下信息: ");
			console.log(data);
		}
	},

	setHandler(type, callback) {
		this.handler[type] = callback;
	}
}

/*
 * 数据存放位置
 */
monitor.store = {
	
	/**
	 * 所有屏幕状态集合
	 * key: sid
	 * value: object
	 * {
	 *    pid: integer | null, 正在播放的工程 ID
	 * }
	 */
	screens : {
		
	},
	
	/**
	 * Array
	 */
	playLists : null,
	
	/**
	 * Object,
	 * key : pid
	 * value : data
	 */
	projects : {},
	
	/**
	 * 获取所有屏幕状态信息
	 */	
	sysncStatusAll : function(data) {
		if (data.result != true) {
			return;
		}
		
		var array = data.data;
		var len = array.length;
		this.screens = {};
		
		for (var i = 0; i < len; i++) {
			var o = array[i];
			this.screens[o.sid] = o;
		}
		
		monitor.msgCenter.release("aside-refresh-button", array);
	},
	
	/**
	 * 选择处理的回调函数, 并让其处理
	 */
	sysncStatus(data) {
		if (data.result != true) {
			return;
		}
		var o = data.data;
		
		this.screens[o.sid] = o;
		monitor.msgCenter.release("status-change", o);
	},
	
	/**
	 * 添加屏幕
	 */
	sysncScreenCreate(data) {
		if (data.result != true) {
			return;
		}
		var o = data.data;
		
		this.screens[o.sid] = o;
		monitor.msgCenter.release("aside-add-button", o);
	},
	
	/**
	 * 删除屏幕
	 */
	sysncScreenDestroy(data) {
		if (data.result != true) {
			return;
		}
		var o = data.data;
		
		delete this.screens[o.sid];
		monitor.msgCenter.release("aside-remove-button", o);
	},
	
	init : function () {
		monitor.store.sysncStatusAll = monitor.store.sysncStatusAll.bind(monitor.store);
		monitor.web.setHandler("sysnc-status-all", monitor.store.sysncStatusAll);
		monitor.store.sysncStatus = monitor.store.sysncStatus.bind(monitor.store);
		monitor.web.setHandler("sysnc-status", monitor.store.sysncStatus);
		monitor.store.sysncScreenCreate = monitor.store.sysncScreenCreate.bind(monitor.store);
		monitor.web.setHandler("sysnc-screen-create", monitor.store.sysncScreenCreate);
		monitor.store.sysncScreenDestroy = monitor.store.sysncScreenDestroy.bind(monitor.store);
		monitor.web.setHandler("sysnc-screen-destroy", monitor.store.sysncScreenDestroy);
		
		monitor.store.handleQueryProject = monitor.store.handleQueryProject.bind(monitor.store);
		monitor.web.setHandler("/query-return/project", monitor.store.handleQueryProject);
		monitor.store.handleQueryAllPlayLists = monitor.store.handleQueryAllPlayLists.bind(monitor.store);
		monitor.web.setHandler("/query-return/playList", monitor.store.handleQueryAllPlayLists);
	},
	
	/**
	 * 结果处理
	 */
	handleQueryAllPlayLists : function (data) {
		monitor.store.playLists = data.data;
		
		monitor.msgCenter.release("query-data-all-playList", data.data);
	},
	
	/**
	 * 通用查询服务: 查询播放列表
	 * @return
	 *   返回工程数据. 如果这里没有缓存相关工程的数据, 则返回 null.
	 *   系统会去服务器端请求数据.
	 *   应用需要在后面监听事件 query-data-all-playList 来取得数据
	 */
	queryAllPlayLists : function () {
		if (this.playLists != null) {
			return this.playLists;
		}
		
		monitor.web.ws.send("/query/playList", {});
		return null;
		
		return new Promise(function (resolve, reject) {
			// 发送
			monitor.web.ws.send("/query/playList", {});
			// 接收
			monitor.web.setHandler("/query-return/playList", function (data) {
				monitor.store.handleQueryAllPlayLists(data);
				resolve(monitor.store.playLists);
			});
		});
	},
	
	/**
	 * 结果处理
	 */
	handleQueryProject : function (data) {
		var pid = data.data.pid;
		monitor.store.projects[pid] = data.data;
		
		monitor.msgCenter.release("query-data-project", data.data);
	},
	
	/**
	 * 通用查询服务: 查询工程数据
	 * @param id
	 *   参数
	 * @return
	 *   返回工程数据. 如果这里没有缓存相关工程的数据, 则返回 null.
	 *   系统会去服务器端请求数据.
	 *   应用需要在后面监听事件 query-data-project 来取得数据
	 * @throw
	 *   当 id 等于 null 时
	 */
	queryProject : function (id) {
		if (id == null) {
			throw {title : "invalid id", id : id};
		}
		
		if (this.projects[id] != null) {
			return this.projects[id];
		}
		
		monitor.web.ws.send("/query/project", {"id" : id});
		return null;
	}
	
}

monitor.store.init();

// 监听启动事件
monitor.msgCenter.subscribe(
		["window-onload"],
function(event, argv) {
	monitor.web.ws = $.websocket("ws://" + location.host + "/aurora/monitor", {
		error : function() {
			console.error("与远程服务器的连接错误");
		},
		open : function(event) {
			console.log("连接服务器成功");
			// 发送消息 type = "/status/sysnc"
			monitor.web.ws.send("/status/sysnc");
		},
		//接收到消息的回调方法
		message : function(msg) {
			var data = eval("(" + msg.originalEvent.data + ")");
			monitor.web.selectHandler(data);
		},
		close : function(event) {
			console.log("与远程服务器的连接关闭");
		}
	});
});
